<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
  <el-card shadow="never" :border="false">
      <template #header>
        <div class="header">
          <span>共 {{total}} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round @click="openDrawer('add')">
            添加面经
          </el-button>
        </div>
      </template>

      <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="stem"
        label="标题"
        width="400">
      </el-table-column>
      <el-table-column
        prop="creator"
        label="作者"
        >
      </el-table-column>
      <el-table-column
        prop="likeCount"
        label="点赞">
      </el-table-column>
      <el-table-column
        prop="views"
        label="浏览">
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="更新日期"
        width="180">
      </el-table-column>
      <el-table-column label="操作" width="120">
       <template  #default="obj">
        <div class="actions">
          <i class="el-icon-view" @click="openDrawer('view',obj.row.id)"></i>
          <i class="el-icon-edit-outline" @click="openDrawer('edit',obj.row.id)"></i>
          <i class="el-icon-delete" @click="del(obj,obj.row.id)"></i>
        </div>
       </template>
      </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
  </el-card>
  <el-drawer
    :title="drawerTitle"
    :visible.sync="drawer"
    :before-close="handleClose"
    size="50%">
    <!-- 添加面经 -->
    <el-form label-width="80px" :model="form" :rules="rules" ref="form" v-if="drawerType!='view'">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content" @blur="$refs.form.validateField('content')"></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
    </el-form>
    <div v-else class="article-preview">
      <h5>{{ form.stem }}</h5>
      <div v-html="form.content"></div>
    </div>
  </el-drawer>

  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { ArticleListAPI, addAritcleAPI, delArticleAPI, articleAPI, updateAPI } from '@/api/article'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      current: 1,
      pageSize: 10,
      list: [],
      total: 0,
      drawer: false,
      drawerTitle: '',
      form: {
        stem: '',
        content: ''
      },
      drawerType: '',
      rules: {
        stem: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.initData()
  },
  methods: {
    async initData () {
      const res = await ArticleListAPI({
        current: this.current,
        pageSize: this.pageSize
      })
      console.log('initData数据', res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    async del (obj, id) {
      console.log('id数据', id)
      console.log('obj数据', obj)
      try {
        await delArticleAPI(id)
        this.$message.success('删除成功')
        this.initData()
      } catch (error) {
        if (error.response) {
          this.$message.error(error.response.data.message)
        } else {
          this.$message.error('添加失败')
        }
      }
    },
    handleSizeChange (val) {
      this.pageSize = val
      this.initData()
    },
    handleCurrentChange (val) {
      this.current = val
      this.initData()
    },
    // 抽屉打开
    async openDrawer (type, id) {
      if (type === 'add') {
        this.drawerTitle = '添加面经'
        this.drawerType = 'detail'
      } else {
        if (type === 'edit') {
          this.drawerTitle = '面经编辑'
          this.drawerType = 'detail'
        } else {
          this.drawerTitle = '面经详情'
          this.drawerType = 'view'
        }
        const res = await articleAPI(id)
        console.log('面经详情', res)
        this.form.content = res.data.content
        this.form.stem = res.data.stem
        this.form.id = res.data.id
      }
      this.drawer = true
      console.log(type)
    },
    handleClose () {
      this.drawer = false
      this.$refs.form?.resetFields()
      this.form = {}
    },
    // 表单提交校验
    onSubmit () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // console.log(this.form)
          if (this.form.id) {
            try {
              const res = await updateAPI(this.form)
              console.log('表单修改结果', res)
              this.initData()
              this.handleClose()
              this.$message.success('修改成功')
            } catch (error) {
              if (error.response) {
                this.$message.error(error.response.data.message)
              } else {
                this.$message.error('修改失败')
              }
            }
          } else {
            try {
              const res = await addAritcleAPI(this.form)
              console.log('表单提交结果', res)
              this.initData()
              this.handleClose()
              this.$message.success('添加成功')
            } catch (error) {
              if (error.response) {
                this.$message.error(error.response.data.message)
              } else {
                this.$message.error('添加失败')
              }
            }
          }
        }
      })
    }
  }

}
</script>
<style lang="scss">
.el-table__row{
  &:hover {
    >td{
      background-color: #8f8e91 !important; /* 浅绿色高亮 */
    }
  }
}
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgb(255, 255, 255);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
